const db = wx.cloud.database({
  env: 'cloud1-2g6vpgs796710242'
})
const app = getApp()
const _ = db.command
var klist = []
var flist = []

Page({
  data: {
    planet_info:[],
    moment:"",
    nicon:["https://636c-cloud1-2g6vpgs796710242-1304238103.tcb.qcloud.la/%E6%95%B0%E5%AD%97-1.png?sign=3931dd46ce32dd93e54fb4c51e59933a&t=1678684846","https://636c-cloud1-2g6vpgs796710242-1304238103.tcb.qcloud.la/%E6%95%B0%E5%AD%97-2.png?sign=378c27ff86b9d8dd7adf0195144863cd&t=1678684857","https://636c-cloud1-2g6vpgs796710242-1304238103.tcb.qcloud.la/%E6%95%B0%E5%AD%97-3.png?sign=182e14c5c9cd8edb72f8124777d938c2&t=1678684867","https://636c-cloud1-2g6vpgs796710242-1304238103.tcb.qcloud.la/%E6%95%B0%E5%AD%97-4.png?sign=5a33e31223b86b7fe56170b203b80d20&t=1678684878","https://636c-cloud1-2g6vpgs796710242-1304238103.tcb.qcloud.la/%E6%95%B0%E5%AD%97-5.png?sign=ef6398a65e1f6a7395cfd01c6c2c2e22&t=1678684886","https://636c-cloud1-2g6vpgs796710242-1304238103.tcb.qcloud.la/%E6%95%B0%E5%AD%97-6.png?sign=00cedfd68a69fbbe47c3dc877bb5b5ac&t=1678684894","https://636c-cloud1-2g6vpgs796710242-1304238103.tcb.qcloud.la/%E6%95%B0%E5%AD%97-7.png?sign=038e63597325d7817eb206043109d9de&t=1678684903","https://636c-cloud1-2g6vpgs796710242-1304238103.tcb.qcloud.la/%E6%95%B0%E5%AD%97-8.png?sign=1b4ef9b0e0584d99ca599114a8e04842&t=1678684911"],
    toprank:[],
    bottomrank:[],
    storage:[]
  },

  onLoad: function (options) {
    var user = wx.getStorage({
      key:'user',
      success: (res)=> {
        console.log("查询用户信息缓存",res.data)
        this.setData({
          storage:res.data
        })
      }
    })
    console.log("planet传值", options)
    klist.splice(0, klist.length)
    flist.splice(0, flist.length)
    var id = options.id
    db.collection('cp_list')
    .doc(id)
    .get()
    .then(res=>{
      console.log(res)
      this.setData({
        planet_info:res.data,
        moment:new Date(Date.parse(new Date())+ 60*60*1000*8).toISOString().substring(0,10)
      })
    })
    .catch(err=>{
      console.log("行星库数据获取失败",err)
    })

    //CTR自增
    db.collection("cp_list")
    .doc(id)
    .update({
      data:{
        CTR: _.inc(1)
      }
    })
    .then(res=>{
      console.log("CTR自增成功",res)
    })
    if (!this.data.storage.name=="") {
      console.log('用户信息已授权',user)
      this.setData({
        hasUserInfo: true,
        userInfo: user,
      })
      console.log("用户信息", this.data.userInfo)
      console.log("全局openid",app.globalData.openid)

      db.collection("User")
      .where({
        _openid:app.globalData.openid
      })
      .get()
      .then(res=>{
        console.log("用户信息",res)
        var aimcp = this.data.planet_info.title
        for (var i = 0; i < res.data[0].common.length; i++) {
          if (res.data[0].common[i].name==aimcp) {
            db.collection("User")
            .where({
              _openid:app.globalData.openid,
              'common.name':aimcp
            })
            .update({
              data:{
                "common.$.count":res.data[0].common[i].count+1
              }
            })
            .then(res=>{
              console.log("使用频次更新成功",res)
            })
            .catch(err=>{
              console.log(err)
            })
          }
        }
      })
    } else {
      console.log("无缓存用户信息")
    }
    // 更新使用频次
    // wx.getStorage({
    //   key:'user',
    //   success: (res)=> {
    //     console.log("查询用户信息缓存",res.data)
    //   },
    //   fail: (err)=> {
    //     console.log("无缓存用户信息",err)
    //   }
    // })

    // wx.cloud.callFunction({
    //   name:'orderBy',
    // })
    db.collection("Rank")
    .aggregate()
    .sort({
      count:-1
    })
    .end()
    .then(res=>{
      //云开发
      console.log('排序数据',res.result)
      //js方法
      console.log('排序数据',res.list)
      for (var i=0; i<res.list.length; i++) {
        if (res.list[i].type=="keyword") {
          klist = klist.concat(res.list[i])
        }
        else if (res.list[i].type=="field") {
          flist = flist.concat(res.list[i])
        }
      }
      this.setData({
        toprank:klist,
        bottomrank:flist
      })
      console.log("klist",this.data.toprank)
    })
    .catch(err=>{
      console.log("调用失败",err)
    })
  },

  search(e) {
    var kw = e.currentTarget.dataset.key
    wx.request({
      header: {
        "content-type": "application/json",
      },
      method: "GET",
      url: "https://xueshu.baidu.com/usercenter/data/paperreco?cmd=batch_get_kw_chart&kw_list="+kw+"&_token=fc55526ac7bb6e3ab419fe69e594a13130b3c2679074fc620eeb6745dc6aa422&_ts=1641370793&_sign=718e5448c7838507e1615c1dd7d60081",
      success: res => {
        console.log("成功", res.data);
        var bat = encodeURIComponent(JSON.stringify(res.data))
        wx.navigateTo({
          url: '../output1/output1?baidu=' + bat
        })
      },
      fail: err => {
        console.log("错误", err);
      }
    })
  },

  Goconference() {
    wx.navigateTo({
      url: '../socials/socials'
    })
  },

  onShareAppMessage: function () {

  }
})